<?php

session_start();

$email = '';

if (!empty($_GET['action']) && $_GET['action'] == 'logout') {
	$_SESSION['email'] = '';
	$_SESSION['password'] = '';
	
	header('location: login.php');
} else if (!empty($_POST)) {
	include_once('db.php');
	
	if (!empty($_POST['email'])) {
		$email = mysql_real_escape_string($_POST['email']);
	}
	
	if (!empty($_POST['password'])) {
		$password = mysql_real_escape_string($_POST['password']);
	}
	
	if ($email && $password) {
		$result = mysql_query(
				"SELECT Email, Type FROM USER WHERE Email='$email' AND "
				. "Password='$password'");
		
		if (mysql_error()) {
			die(mysql_error());
		} else {
			if (mysql_num_rows($result) != 0) {
				$_SESSION['email'] = $email;
				$_SESSION['password'] = $password;
				
				$user = mysql_fetch_assoc($result);
				
				switch ($user['Type']) {
					case 'R':
						header('location: dashboard.php');
						break;
					case 'A':
						header('location: employee_management.php');
						break;
					default:
						$error = 'User type is not set!';
				}
			} else {
				$error = 'Invalid username/password combination';
			}
		}
	} else {
		$error = 'Enter both a username and password';
	}
}

?>
		

<html>
<head>
	<title>GT Project Manager - Login</title>
	<link rel="stylesheet" type="text/css" href="main.css" />
	<style type="text/css">
		img {
			width: 125px;
			margin-top: -18px;
			float: left;
		}
		
		td.label {
			width: 230px;
			text-align: right;
			font-size: 10pt;
			padding-right: 6px;
		}
		
		input.field {
			width: 300px;
		}
		
		#title {
			font-size: 32px;	
			font-weight: bold;
			vertical-align: middle;
			text-align: center;
		}
		
		#title div {
			width: 350px;
			margin: auto;
			margin-top: 18px;
			margin-bottom: 18px;
		}
	</style>
	<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function() {
			$('#email').focus();
		});
	</script>
</head>
<body>
	<form action="#" method="post">
		<table cellspacing="0" cellpadding="0" id="main">
			<tr><td colspan="2" id="title">
				<div>
					<img src="buzz.gif" />
					GT Project<br />Manager
				</div>
			</td></tr>
			<?php
			if ($error) {
			?>
			<tr><td class="message" colspan="2"><?php echo $error ?></td></tr>
			<?php
			}
			?>
			<tr>
				<td class="label">Email:</td>
				<td class="field">
					<input type="text" name="email" id="email" class="field" value="<?php echo $email ?>" />
				</td>
			</tr>
			<tr>
				<td class="label">Password:</td>
				<td class="field">
					<input type="password" name="password" class="field" />
				</td>
			</tr>
			<tr>
				<td class="button_bar" colspan="2">
					<input type="submit" value="Login" />
				</td>
			</tr>
		</table>
	</form>
</body>
</html>